import scrollOpt from '../base/scrollOptimize'
import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { Sdk_Platform, WX_AD_CONFIG, VIVO_AD_CONFIG, OPPO_AD_CONFIG, Ad_Type } from '../../frame/constants';

cc.Class({
    extends: scrollOpt,

    properties: {
        goldLab: cc.Label,
        starLab: cc.Label,
        bagLab: cc.Label,
        topNode: cc.Node,
        bottomNode: cc.Node,
        oppoBottomNode: cc.Node,
    },
    initPage() {
        if (!he.pool.getPool('bagItem')) {
            he.pool.createPoolSync('bagItem', 5, cc.Prefab, 'uimod/bagItem')
        }
    },

    updatePage() {
        this.scrollInit()
        this.openListen()

        this.time = 0
        this.bagIndex = 0
        this.itemIndex = 0
        this.canUpdate = false
        this.pageInit()

        this.adSwitch(true)

    },

    closeUI() {
        this.time = 0
        this.bagIndex = 0
        this.itemIndex = 0
        this.canUpdate = false
        for (let i = (this.content.children.length); i > 0; i--) {
            he.pool.putElement('bagItem', this.content.children[i - 1])
        }
        this.closeListen()

        for (let j = (this.topNode.children.length); j > 0; j--) {
            this.topNode.children[j - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.topNode.children[j - 1])
        }

        for (let k = (this.bottomNode.children.length); k > 0; k--) {
            this.bottomNode.children[k - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.bottomNode.children[k - 1])
        }
        this.adSwitch(false)
    },

    topSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.topNode
            pre.getComponent('scrollAd').init(config.creatives)
        }
    },

    adSwitch(status) {
        if (status) {
            let guideList = he.local.getJson('guideList', 'user')
            if (!guideList[6]) {
                return
            }
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[10], this.topSet.bind(this))
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[6], this.bottomSet.bind(this))
                    he.ad.interstitialShow()
                    break
                case Sdk_Platform.VIVO:
                    // this.bottomNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.bottomNode)
                    // he.ad.interstitialShow()

                    let num3 = Math.ceil(Math.random() * 10)//20 的几率弹出
                    if (num3 < 2) {
                        gg.uiManager.addPage(UI_Layer.Click, {
                            name: 'randomCardPage',
                            cover: true,
                        })
                        break
                    }
                    break
                case Sdk_Platform.OPPO:

                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 2,
                        position: cc.Vec2(0, -470)
                    })
                    let num = Math.ceil(Math.random() * 10)//20 的几率弹出
                    if (num < 2) {
                        gg.uiManager.addPage(UI_Layer.Click, {
                            name: 'randomCardPage',
                            cover: true,
                        })
                        break
                    }
                    // this.oppoBottomNode.name = OPPO_AD_CONFIG.bannerId[3]
                    // he.ad.changeBanner(status, this.oppoBottomNode)
                    break
                case Sdk_Platform.UC:
                    let num4 = Math.ceil(Math.random() * 10)//20 的几率弹出
                    if (num4 < 2) {
                        gg.uiManager.addPage(UI_Layer.Click, {
                            name: 'randomCardPage',
                            cover: true,
                        })
                        break
                    }
                    let num5 = Math.ceil(Math.random() * 10)
                    if (num5 < 5) {
                        // he.ad.showBanner(this.bottomNode.getComponent(cc.Widget).bottom)
                    } else {
                        he.ad.showInterstitialAd()
                    }
                    break
                case Sdk_Platform.HUAWEI:
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'hwAdPage',
                        cover: false,
                        isShowAd: true,
                        adType: Ad_Type.INSERT,
                        position: 0
                    })
                    break
                default:
                    let num2 = Math.ceil(Math.random() * 10)//20 的几率弹出
                    if (num2 < 2) {
                        gg.uiManager.addPage(UI_Layer.Click, {
                            name: 'randomCardPage',
                            cover: true,
                        })
                        break
                    }
                    break
            }
        } else {
            //  关掉广告
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    break
                case Sdk_Platform.VIVO:
                    // this.bottomNode.name = VIVO_AD_CONFIG.bannerId[0]
                    // he.ad.changeBanner(status, this.bottomNode)
                    break
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    // this.oppoBottomNode.name = OPPO_AD_CONFIG.bannerId[3]
                    // he.ad.changeBanner(status, this.oppoBottomNode)
                    break
                case Sdk_Platform.UC:
                    he.ad.closeBanner()
                    break
                case Sdk_Platform.HUAWEI:
                    if (gg.uiManager.getPage(UI_Layer.Msg, 'hwAdPage')) {
                        gg.uiManager.closePage(UI_Layer.Msg, 'hwAdPage')
                    }
                    break
            }
        }
    },
    bottomSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.bottomNode
            pre.getComponent('scrollAd').init(config.creatives, 1, true)
        }
    },

    pageInit() {
        this.bagNum = he.local.getJson('bagNum', 'user') || 20
        let gold = he.local.getJson('coin', 'user')
        this.bag = he.local.getJson('bag', 'user') || []
        let diamondNum = 0
        let zanBag = []
        this.bagNum = this.bagNum >= this.bag.length ? this.bagNum : this.bag.length

        if (this.bag && this.bag.length > 0) {
            for (let i = 0; i < this.bag.length; i++) {
                if (this.bag[i].id == 20002) {
                    diamondNum = this.bag[i].num
                }
                let baginfo = he.db.getDataByArgs('Item', { id: this.bag[i].id })
                if (baginfo) {
                    if (baginfo.showBag) {
                        zanBag.push(this.bag[i])
                    }
                }
            }
        }
        this.bag = zanBag

        this.itemNum = this.bag.length
        // let 
        this.canUpdate = true
        this.goldLab.string = gold
        this.starLab.string = diamondNum
        this.bagLab.string = `${this.bag.length}/${this.bagNum}`
        // this.bagInit(this.bagNum)

        this.bagItemIndex = -1
        gg.eventManager.on('bagItem', this.listenClient, this)

    },

    listenClient(code, param) {
        switch (code.type) {
            case UI_Listen.BAG_CHOOSE:
                this.chooseItem(code.index, code.info)
                break
        }
    },

    chooseItem(index, info) {
        let bagItemxx = this.content.children[this.bagItemIndex]
        if (bagItemxx) {
            if (this.bagItemIndex != index) {
                if (this.bagItemIndex == -1) {
                } else {
                    bagItemxx.getComponent('bagItem').hideLight()
                }
                this.bagItemIndex = index
                let newItem = this.content.children[index]
                if (newItem) {
                    newItem.getComponent('bagItem').showLight()
                }
            } else {
                bagItemxx.getComponent('bagItem').hideLight()
                this.bagItemIndex = -1
            }
        }

    },


    bagInit(num) {
        for (let i = 0; i < num; i++) {
            let pre = he.pool.getElement('bagItem')
            if (pre) {
                pre.parent = this.content
                pre.getComponent('bagItem').hideItem()
                pre.getComponent('bagItem').hideLight()
            }
        }
    },

    // itemInit() {
    //     let bag = he.local.getJson('bag', 'user')
    //     if (bag && bag.length > 0) {
    //         for (let j = 0; j < bag.length; j++) {
    //             // this.bagItemInit(j, bag[j])
    //         }
    //     }
    // },

    bagItemInit(index, info) {
        //记得添加数据
        if (this.canUpdate) {
            let spt = this.content.children[index].getComponent('bagItem')
            spt.initInfo(parseInt(info.id), 1, index)
            spt.setNum(info.num)
            spt.canShowDetail()
        }
    },
    //根据物品信息展示相应的二级页面
    showBagItemInfo(info) {
    },

    update(dt) {
        if (this.canUpdate) {
            this.time += dt
            if (this.time >= 0.01) {
                this.time = 0
                if (this.bagIndex < this.bagNum) {
                    let pre = he.pool.getElement('bagItem')
                    pre.parent = this.content
                    let spt = pre.getComponent('bagItem')
                    spt.initInfo(null, null, this.bagIndex)
                    spt.hideItem()
                    spt.hideLight()
                    this.bagIndex += 1
                    if (this.itemIndex < this.itemNum) {
                        this.bagItemInit(this.itemIndex, this.bag[this.itemIndex])
                        this.itemIndex += 1
                    }
                }
            }
        }
    },

});
